package com.zy.experience_system.controller;


import com.zy.experience_system.enums.PermsEnum;
import com.zy.experience_system.enums.ResultEnum;
import com.zy.experience_system.exception.CommonException;
import com.zy.experience_system.form.*;
import com.zy.experience_system.service.TestService;
import com.zy.experience_system.service.TestSetService;
import com.zy.experience_system.util.ResultUtil;
import com.zy.experience_system.vo.ResultVO;
import com.zy.experience_system.vo.TestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/test")
public class TestController extends BaseController {

    @Autowired
    private TestService testService;
    @Autowired
    private TestSetService testSetService;


    @GetMapping("/all")
    public ResultVO all() throws Exception {
        if (!claims.get("perms").equals(PermsEnum.STUDENT.getPerms())) {
            try {
                List<TestVO> testVOList = testService.findAll();
                return ResultUtil.success(testVOList);
            } catch (Exception e) {
                return ResultUtil.error("查询失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @GetMapping("/find/{testId}")
    public ResultVO find(
            @PathVariable("testId") Integer testId) throws Exception {
        try {
            TestVO testVO = testService.findByTestId(testId);
            return ResultUtil.success(testVO);
        } catch (Exception e) {
            return ResultUtil.error("查询失败");
        }
    }

    @PostMapping("/create")
    public ResultVO create(
            @Validated @RequestBody TestForm testForm) throws Exception {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            try {
                Integer testId = testService.save(testForm);
                Map<String, Integer> map = new HashMap<>();
                map.put("testId", testId);
                return ResultUtil.success(map);
            } catch (Exception e) {
                return ResultUtil.error("创建失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }


    @PutMapping("/update")
    public ResultVO update(
            @Validated @RequestBody TestForm testForm) throws Exception {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            if (claims.getId().equals(testForm.getTeacher())) {
                try {
                    testService.save(testForm);
                    return ResultUtil.success(null);
                } catch (Exception e) {
                    System.out.println(e.toString());
                    return ResultUtil.error("修改失败");
                }
            }
            throw new CommonException(ResultEnum.NO_PERMS);
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @GetMapping("/findByTeacher/{teachId}")
    public ResultVO find(
            @PathVariable("teachId") String teachId) throws Exception {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            if (claims.getId().equals(teachId)){
                try {
                    List<TestVO> testVOList = testService.findAllByTeachId(teachId);
                    return ResultUtil.success(testVOList);
                } catch (Exception e) {
                    return ResultUtil.error("查询失败");
                }
            }
            throw new CommonException(ResultEnum.NO_PERMS);
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @DeleteMapping("/delete")
    public ResultVO delete(
            @Validated @RequestBody TestAndTeachForm testAndTeachForm) throws Exception{
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())){
            if (claims.getId().equals(testAndTeachForm.getTeachId())){
                try {
                    testService.delete(testAndTeachForm.getTestId());
                    return ResultUtil.success(null);
                }catch (Exception e){
                    return ResultUtil.error("删除失败");
                }
            }
            throw new CommonException(ResultEnum.NO_PERMS);
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @PostMapping("/addToSet")
    public ResultVO addToSet(
            @Validated @RequestBody SetAndTestForm setAndTestForm) throws Exception{
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())){
            Integer setId = setAndTestForm.getSet();
            String teachId = testSetService.find(setId).getTeachId();
            if (claims.getId().equals(teachId)){
                try {
                    testService.save(setAndTestForm);
                    return ResultUtil.success(null);
                }catch (Exception e){
                    System.out.println(e.toString());
                    return ResultUtil.error("添加失败");
                }
            }
            throw new CommonException(ResultEnum.NO_PERMS);
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @DeleteMapping("/deleteFromSet")
    public ResultVO deleteFromSet(
            @Validated @RequestBody TestAndSetForm testAndSetForm) throws Exception{
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())){
            if (claims.getId().equals(testAndSetForm.getTeachId())){
                try {
                    testService.delete(testAndSetForm.getTestId(),testAndSetForm.getSetId());
                    return ResultUtil.success(null);
                }catch (Exception e){
                    return ResultUtil.error("删除失败");
                }
            }
            throw new CommonException(ResultEnum.NO_PERMS);
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

}
